<?php

declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\SystemSmChannel as SystemSmChannelModel;

class SystemSMChannel extends BaseService
{
    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        //
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }

    /**
     * 获取列表数据
     * @param $param
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-25 14:37:06
     * author: jiang
     */
    public function index($param, $page, $limit)
    {
        $arrData = ['code' => 0, 'message' => 'success'];
        $where = [['is_del', '=', 0]];
        if ($param['name']) {
            $where[] = ['name', 'like', '%' . $param['name'] . '%'];
        }
        if ($param['status'] >= 0) {
            $where[] = ['status', '=', $param['status']];
        }

        if (!is_null($param['start_time'])) {
            $where[] = ['create_time', '>=', $param['start_time']];
        }
        if ($param['end_time']) {
            $where[] = ['create_time', '<', $param['end_time']];
        }

        $oSystemSmChannelModel = SystemSmChannelModel::with(['get_creator_name', 'get_update_name'])->where($where);
        $total = $oSystemSmChannelModel->count();
        $result = $oSystemSmChannelModel->page($page, $limit)->order('id', 'desc')->select();
        foreach ($result as &$value) {
            $value->creator = $value->creator_value;
            unset($value->creator_value);
            $value->updater = $value->updater_value;
            unset($value->updater_value);
        }
        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }

    /**
     * 批量修改状态
     * @param $param
     * @return array
     * 2024-10-25 15:47:31
     * author: jiang
     */
    public function status($param)
    {
        $result = SystemSmChannelModel::whereIn('id', $param['ids'])->update([
            'status' => $param['status'],
            'update_time' => date('Y-m-d H:i:s'),
            'updater' => $param['operator_id']
        ]);
        if (!$result) {
            return ['code' => 1, 'message' => '操作失败！'];
        }
        $this->logger->logOperation($param['ids'], 'SystemSMChannel', '修改', '批量修改状态');
        return ['code' => 0, 'message' => '操作成功！'];
    }

    /**
     * 添加数据
     * @param $param
     * @return array
     * 2024-10-25 16:20:15
     * author: jiang
     */
    public function save($param)
    {
        // 判断name是否重复
        $count = SystemSmChannelModel::where([['name', '=', $param['name']], ['is_del', '=', 0]])->count();
        if ($count > 0) {
            return ['code' => 1, 'message' => '名称重复！'];
        }
        $oSystemSmChannelModel = new SystemSmChannelModel();
        $oSystemSmChannelModel->name = $param['name'];
        $oSystemSmChannelModel->max_characters_num = $param['max_characters_num'];
        $oSystemSmChannelModel->status = $param['status'];
        $oSystemSmChannelModel->is_del = 0;
        $oSystemSmChannelModel->creator = $param['operator_id'];
        $oSystemSmChannelModel->create_time = date('Y-m-d H:i:s');
        if (!$oSystemSmChannelModel->save()) {
            return ['code' => 1, 'message' => '添加失败！'];
        }
        $this->logger->logOperation($oSystemSmChannelModel->id, 'SystemSMChannel', '添加', '添加数据');
        return ['code' => 0, 'message' => '添加成功！'];
    }

    /**
     * 修改数据
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-25 16:24:19
     * author: jiang
     */
    public function update($param)
    {
        $oSystemSmChannelModel = SystemSmChannelModel::find($param['id']);
        if (!$oSystemSmChannelModel) {
            return ['code' => 1, 'message' => '数据不存在！'];
        }
        // 判断name是否重复
        $count = SystemSmChannelModel::where([['name', '=', $param['name']], ['is_del', '=', 0]])->where('id', '<>', $param['id'])->count();
        if ($count > 0) {
            return ['code' => 1, 'message' => '名称重复！'];
        }
        $oSystemSmChannelModel->name = $param['name'];
        $oSystemSmChannelModel->max_characters_num = $param['max_characters_num'];
        $oSystemSmChannelModel->status = $param['status'];
        $oSystemSmChannelModel->updater = $param['operator_id'];
        $oSystemSmChannelModel->update_time = date('Y-m-d H:i:s');
        if (!$oSystemSmChannelModel->save()) {
            return ['code' => 1, 'message' => '修改失败！'];
        }
        $this->logger->logOperation($oSystemSmChannelModel->id, 'SystemSMChannel', '修改', '修改数据');
        return ['code' => 0, 'message' => '修改成功！'];
    }

    /**
     * 删除数据
     * @param $param
     * @return array
     * 2024-10-25 16:26:03
     * author: jiang
     */
    public function delete($param)
    {
        $result = SystemSmChannelModel::whereIn('id', $param['ids'])->update([
            'is_del' => time(),
            'update_time' => date('Y-m-d H:i:s'),
            'updater' => $param['operator_id']
        ]);
        if (!$result) {
            return ['code' => 1, 'message' => '删除失败！'];
        }
        $this->logger->logOperation($param['ids'], 'SystemSMChannel', '删除', '删除数据');
        return ['code' => 0, 'message' => '删除成功！'];
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2024-10-28 11:48:17
     * @param [type] @required
     * @return Renderable
     */
    public function getSelectData($where = ['is_del' => 0])
    {

        $result = SystemSmChannelModel::where($where)->field('id as value,name as label')->select();
        $res = $result->toArray();
        return [
            'code' => 0,
            'data' => $res
        ];
    }
}
